package com.weds.ticket.ticket.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

/**
 * @Description TODO
 * @Author lihw
 * @Date 2025/8/14 下午5:25
 */
@Service
public class StockService {

    private final RestTemplate restTemplate = new RestTemplate();
    private final ObjectMapper objectMapper = new ObjectMapper();

    // 获取股票实时数据
    public void fetchStockData(String stockCode) {
        // 构建请求URL
        String marketPrefix = stockCode.startsWith("6") ? "1." : "0.";
        if (stockCode.equals("000001")) {
            marketPrefix = "1.";
        }

        /**
         *
         */

        String url = "https://push2.eastmoney.com/api/qt/stock/get?secid="
                + marketPrefix + stockCode
                + "&fields=f12,f14,f43,f44,f45,f46,f60,f58,f47,f48,f170,f171,f116,f162,f185";

        try {
            // 发送HTTP请求
            String response = restTemplate.getForObject(url, String.class);

            // 解析JSON响应
            JsonNode root = objectMapper.readTree(response);
            JsonNode data = root.path("data");

            // 提取关键信息
            String code = data.path("f12").asText();      // 股票代码
            String name = data.path("f58").asText();      // 股票名称
            double currentPrice = data.path("f43").asDouble() / 100; // 当前价格
            double highPrice = data.path("f44").asDouble() / 100;    // 最高价
            double lowPrice = data.path("f45").asDouble() / 100;     // 最低价
            long volume = data.path("f46").asLong();      // 成交量(股)
            double prevClose = data.path("f60").asDouble() / 100;    // 昨日收盘价

            // 计算涨跌幅
            double changePercent = ((currentPrice - prevClose) / prevClose) * 100;

            // 格式化涨跌幅符号和颜色标记
            String changeSymbol = changePercent >= 0 ? "+" : "";
            String colorCode = changePercent >= 0 ? "\u001B[32m" : "\u001B[31m"; // 绿色/红色

            if (!StringUtils.hasLength(name)) {
                name = "-";
            }
            if (!StringUtils.hasLength(code)) {
                code = stockCode;
            }

            // 构建单行输出
            String result = String.format(
                    "%-6s %-10s %.2f元 (%s%.2f%%) | 高:%.2f 低:%.2f | 量:%s",
                    code,
                    name,
                    currentPrice,
                    (changePercent >= 0 ? "+" : ""), // 保留涨跌幅符号
                    changePercent,
                    highPrice,
                    lowPrice,
                    formatVolume(volume)
            );
            System.out.println(result);


        } catch (Exception e) {
            System.out.println("获取失败");
        }
    }

    // 格式化成交量显示
    private String formatVolume(long volume) {
        if (volume >= 100000000) {
            return String.format("%.2f亿股", volume / 100000000.0);
        } else if (volume >= 10000) {
            return String.format("%.2f万股", volume / 10000.0);
        }
        return volume + "股";
    }
}
